parameters:
  buildConfig: 'Debug'
  osGroup: ''
  osSubGroup: ''
  platform: ''
  container: ''
  timeoutInMinutes: ''
  variables: {}
  pool: ''
  condition: true
  isOfficialBuild: false

### Product build
jobs:
- template: /eng/pipelines/common/templates/runtimes/xplat-job.yml
  parameters:
    buildConfig: ${{ parameters.buildConfig }}
    osGroup: ${{ parameters.osGroup }}
    osSubGroup: ${{ parameters.osSubGroup }}
    helixType: 'build/product/'
    enableMicrobuild: true
    pool: ${{ parameters.pool }}
    condition: ${{ parameters.condition }}

    # Compute job name from template parameters
    name: ${{ format('mono_{0}{1}_offsets', parameters.osGroup, parameters.osSubGroup) }}
    displayName: ${{ format('Mono {0}{1} AOT offsets', parameters.osGroup, parameters.osSubGroup) }}

    # Run all steps in the container.
    # Note that the containers are defined in platform-matrix.yml
    container: ${{ parameters.container }}

    timeoutInMinutes: ${{ parameters.timeoutInMinutes }}

    gatherAssetManifests: true
    variables:
    - name: osGroup
      value: ${{ parameters.osGroup }}
    - name: osSubGroup
      value: ${{ parameters.osSubGroup }}
    - name: officialBuildIdArg
      value: ''
    - ${{ if eq(parameters.isOfficialBuild, true) }}:
      - name: officialBuildIdArg
        value: '/p:OfficialBuildId=$(Build.BuildNumber)'
    - name: osOverride
      value: -os linux
    - name: archType
      value: x64
    - ${{ parameters.variables }}

    steps:

    # Install native dependencies
    # Linux builds use docker images with dependencies preinstalled,
    # and FreeBSD builds use a build agent with dependencies
    # preinstalled, so we only need this step for OSX and Windows.
    - ${{ if in(parameters.osGroup, 'osx', 'maccatalyst', 'ios', 'iossimulator', 'tvos', 'tvossimulator') }}:
      - script: $(Build.SourcesDirectory)/eng/install-native-dependencies.sh $(osGroup)
        displayName: Install native dependencies

    # Build
    - ${{ if ne(parameters.osGroup, 'windows') }}:
      - script: ./build$(scriptExt) -subset mono.aotcross -c $(buildConfig) -arch $(archType) $(osOverride) -ci $(officialBuildIdArg) /p:MonoGenerateOffsetsOSGroups=$(osGroup)
        displayName: Generate AOT offsets
    - ${{ if eq(parameters.osGroup, 'windows') }}:
      - script: build$(scriptExt) -subset mono.aotcross -c $(buildConfig) -arch $(archType) $(osOverride) -ci $(officialBuildIdArg) /p:MonoGenerateOffsetsOSGroups=$(osGroup)
        displayName: Generate AOT offsets

    # Upload offset files
    - task: CopyFiles@2
      displayName: Collect offset files
      inputs:
        sourceFolder: '$(Build.SourcesDirectory)/artifacts/obj/mono/'
        contents: '**/offsets-*.h'
        targetFolder: '$(Build.SourcesDirectory)/artifacts/obj/mono/offsetfiles/'

    - task: PublishPipelineArtifact@1
      displayName: Upload offset files
      inputs:
        targetPath: '$(Build.SourcesDirectory)/artifacts/obj/mono/offsetfiles'
        artifactName: 'Mono_Offsets_$(osGroup)$(osSubGroup)'

    # Publish Logs
    - task: PublishPipelineArtifact@1
      displayName: Publish Logs
      inputs:
        targetPath: $(Build.SourcesDirectory)/artifacts/log
        artifactName: 'BuildLogs_Attempt$(System.JobAttempt)_Mono_Offsets_$(osGroup)$(osSubGroup)'
      continueOnError: true
      condition: always()
